# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ignacio Casal Quinteiro <icq@gnome.org>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=glib2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.66.4
pkgrel=1
url="https://www.gtk.org/"
arch=('any')
pkgdesc="Common C routines used by GTK+ 3 and other libs (mingw-w64)"
license=(LGPL2)
install=glib2-${CARCH}.install
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-pcre"
         "${MINGW_PACKAGE_PREFIX}-libffi"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=("https://download.gnome.org/sources/glib/${pkgver%.*}/glib-${pkgver}.tar.xz"
        0001-Update-g_fopen-g_open-and-g_creat-to-open-with-FILE_.patch
        0001-win32-Make-the-static-build-work-with-MinGW-when-pos.patch
        0001-disable-some-tests-when-static.patch
        glib-compile-schemas.hook.in
        gio-querymodules.hook.in
        0002-disable_glib_compile_schemas_warning.patch
        pyscript2exe.py)
sha256sums=('97df8670e32f9fd4f7392b0980e661dd625012015d58350da1e58e343f4af984'
            '51d02360a1ee978fd45e77b84bca9cfbcf080d91986b5c0efe0732779c6a54ec'
            '1e3ac7cfd4644f3849704e54fcfbb12d15440a33cd5c2d014d4a479c6aaab185'
            '0f44135a139e3951c4b5fa7d4628d75226e0666d891faf524777e1d1ec3b440b'
            '0c3d54636407e0e13429b73959cace626253cd772e1d4870de0fb92b0b99545a'
            'f18d27d98709dba8c5f9756672baaf0158fb4353c9edbdc2e80021f88ff34ced'
            '396c25cfd740ffbb72209133c7b9453173167577265a4bb14502678de8d5a8f9'
            '00e03a8b9d45e620c6fabbf96f5ccb41d1e49cc9b35ccb846d423f235ad5bec6')

prepare() {
  cd "${srcdir}/glib-${pkgver}"

  patch -Np1 -i "${srcdir}"/0001-Update-g_fopen-g_open-and-g_creat-to-open-with-FILE_.patch
  patch -Np1 -i "${srcdir}"/0001-win32-Make-the-static-build-work-with-MinGW-when-pos.patch
  patch -Np1 -i "${srcdir}"/0001-disable-some-tests-when-static.patch
  patch -Np1 -i "${srcdir}"/0002-disable_glib_compile_schemas_warning.patch
}

build() {
  msg "Build static version"
  [[ -d "${srcdir}"/build-${CARCH}-static ]] && rm -rf "${srcdir}"/build-${CARCH}-static
  mkdir "${srcdir}"/build-${CARCH}-static && cd "${srcdir}"/build-${CARCH}-static

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("--buildtype=release")
  else
    extra_config+=("--buildtype=debug")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  meson \
    --prefix="${MINGW_PREFIX}" \
    "${extra_config[@]}" \
    --default-library=static \
    -Dforce_posix_threads=true \
    "../glib-${pkgver}"
  ninja

  msg "Build shared version"
  [[ -d "${srcdir}"/build-${CARCH}-shared ]] && rm -rf "${srcdir}"/build-${CARCH}-shared
  mkdir "${srcdir}"/build-${CARCH}-shared && cd "${srcdir}"/build-${CARCH}-shared

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  meson \
    --prefix="${MINGW_PREFIX}" \
    "${extra_config[@]}" \
    -Ddefault_library=shared \
    -Dman=true \
    -Dforce_posix_threads=true \
    -Dgtk_doc=false \
    "../glib-${pkgver}"

  ninja
}

package() {
  cd "${srcdir}/build-${CARCH}-static"
  DESTDIR="${pkgdir}" ninja install

  cd "${srcdir}/build-${CARCH}-shared"
  DESTDIR="${pkgdir}" ninja install

  for hook in glib-compile-schemas gio-querymodules; do
    local hook_path="${srcdir}/${MINGW_PACKAGE_PREFIX}-${hook}.hook";
    cp "${srcdir}/${hook}.hook.in" "${hook_path}"
    sed -s "s|@MINGW_HOOK_TARGET_PREFIX@|${MINGW_PREFIX:1}|g" -i "${hook_path}"
    sed -s "s|@MINGW_PREFIX@|${MINGW_PREFIX}|g" -i "${hook_path}"
    install -Dt "$pkgdir/usr/share/libalpm/hooks" -m644 "${hook_path}"
  done

  install -Dm644 "${srcdir}/glib-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"

  local _PRE_WIN="$(cygpath -m ${MINGW_PREFIX})"
  for name in glib-mkenums glib-genmarshal gdbus-codegen gtester-report; do
    ${MINGW_PREFIX}/bin/python3 \
      "${srcdir}/pyscript2exe.py" "${pkgdir}${MINGW_PREFIX}/bin/${name}"
    sed -e "s|${_PRE_WIN}|${MINGW_PREFIX}|g" \
     -i ${pkgdir}${MINGW_PREFIX}/bin/${name}-script.py
  done
}
